/**
 * 
 */
package gui.customControl;

import gui.util.IconFactory;

import java.awt.Dialog;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import data.Player;
import data.Position;
import data.Data.Direction;
import engine.GameEngine;

/**
 * @author Bogdan
 *
 */
public class PushActionDialog extends JDialog 
{
	private static final long serialVersionUID = -5919319547347902610L;

	public PushActionDialog(JFrame parent, Player center, Player pusher, ArrayList<Direction> activeButtons)
	{
		super(parent, "Select push destination", Dialog.ModalityType.DOCUMENT_MODAL);
		initializeComponets(center, pusher);
		activateButtons(activeButtons);
		setDefaultCloseOperation(HIDE_ON_CLOSE);

		m_center = center;
		
		pack();
		setVisible(false);
	}

	/**
	 * Gets the selected position.
	 * @return Returns a Position
	 */
	public Position getPosition() 
	{
		setVisible(true);
		
		return GameEngine.getInstance().convertDirectionToPosition(m_center, m_direction);
	}

	private void initializeComponets(Player center, Player pusher) 
	{
		m_nw = new JButton(IconFactory.instance().getDirectionIcon(Direction.NW));
		m_n  = new JButton(IconFactory.instance().getDirectionIcon(Direction.N));
		m_ne = new JButton(IconFactory.instance().getDirectionIcon(Direction.NE));
		m_w  = new JButton(IconFactory.instance().getDirectionIcon(Direction.W));
		m_e  = new JButton(IconFactory.instance().getDirectionIcon(Direction.E));
		m_sw = new JButton(IconFactory.instance().getDirectionIcon(Direction.SW));
		m_s  = new JButton(IconFactory.instance().getDirectionIcon(Direction.S));
		m_se = new JButton(IconFactory.instance().getDirectionIcon(Direction.SE));
		
		ActionListener listener = new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				JButton source = (JButton)e.getSource();
				if(source.equals(m_nw))
					m_direction = Direction.NW;
				else if(source.equals(m_n))
					m_direction = Direction.N;
				else if(source.equals(m_ne))
					m_direction = Direction.NE;
				else if(source.equals(m_w))
					m_direction = Direction.W;
				else if(source.equals(m_e))
					m_direction = Direction.E;
				else if(source.equals(m_sw))
					m_direction = Direction.SW;
				else if(source.equals(m_s))
					m_direction = Direction.S;
				else if(source.equals(m_se))
					m_direction = Direction.SE;
				
				dispose();
			}
		};
		
		m_nw.addActionListener(listener);
		m_n.addActionListener(listener);
		m_ne.addActionListener(listener);
		m_w.addActionListener(listener);
		m_e.addActionListener(listener);
		m_sw.addActionListener(listener);
		m_s.addActionListener(listener);
		m_se.addActionListener(listener);
		
		JPanel panel = (JPanel) getContentPane();
		panel.setLayout(new GridLayout(3, 3));
		panel.add(m_nw);
		panel.add(m_n);
		panel.add(m_ne);
		panel.add(m_w);
		panel.add(new JLabel(center.name()));
		panel.add(m_e);
		panel.add(m_sw);
		panel.add(m_s);
		panel.add(m_se);
	}
	
	/**
	 * Activate only the selected buttons
	 * @param activeButtons - the list of button directions to activate.
	 */
	private void activateButtons(ArrayList<Direction> activeButtons) 
	{
		// disable the buttons
		m_nw.setEnabled(false);
		m_n.setEnabled(false);
		m_ne.setEnabled(false);
		m_w.setEnabled(false);
		m_e.setEnabled(false);
		m_sw.setEnabled(false);
		m_s.setEnabled(false);
		m_se.setEnabled(false);
		
		// activate only the selected buttons
		for(Direction d : activeButtons)
		{
			switch(d)
			{
			case NW: m_nw.setEnabled(true); break;
			case N:  m_n.setEnabled(true); break;
			case NE: m_ne.setEnabled(true); break;
			case W:  m_w.setEnabled(true); break;
			case E:  m_e.setEnabled(true); break;
			case SW: m_sw.setEnabled(true); break;
			case S:  m_s.setEnabled(true); break;
			case SE: m_se.setEnabled(true); break;
			}
		}
	}
	
	private JButton m_nw;
	private JButton m_n;
	private JButton m_ne;
	private JButton m_w;
	private JButton m_e;
	private JButton m_sw;
	private JButton m_s;
	private JButton m_se;
	
	private Direction m_direction;
	private Position m_center;
}
